/*
 * Created on 2006-2-20
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.hitrust.gs.net;

import com.hitrust.gs.util.exception.*;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.lib.GSSystemConfigurator;
import com.hitrust.gs.util.date.*;
/**
 * @author simonyi
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class GSAuditFlowManager extends Thread {

    private Download download = new Download();
    private GSAuditDAO dao = new GSAuditDAO();
    private GSAuditManager manager = new GSAuditManager();
    
    private String lastDate = "";
    
    private boolean processResult = true;//当日处理结果
    
    private int processNum = 0;
    
    private static GSAuditFlowManager fm = new GSAuditFlowManager();
    
    public static GSAuditFlowManager getInstance(){
        return fm;
    }
    
    private GSAuditFlowManager(){

        System.out.println("\n*****XXXXXGSAuditFlowManager is runningXXXXX*****\n");
        start();
    
    }
    
    public void run(){
        try{
            while(true){
                String currTime = DateUtil.getCurrentTime("T","AD");
                String currDate = GSAuditConstants.getCurrentDate();
                //如果当日已经执行过操作
                if(currDate.equals(lastDate) && processResult){
                    sleep(1000 * 60 * 60);//休眠60分钟
                }else if(dao.isTodayAuditFinished(currDate)){
                    //如果数据库里已经有记录说明当天操作已经执行，休眠60分钟
                    sleep(1000 * 60 * 60);
                }
                else{
                    //超过指定的处理时间
                    if(Integer.parseInt(currTime) > Integer.parseInt(GSSystemConfigurator.getParam("gs.auth.processtime"))){
                        //更新系统记录的当日操作的处理时间
                        lastDate = currDate;
                        processResult = false;
                        //执行动作
                        processResult = execute(currDate);         
                        if(processResult)
                            processNum = 0;
                        
                    }
                    sleep(600000);//处理完毕，休眠10分钟，用于在下载不成功时的缓冲
                }
            }
        }catch(Exception e){
            
        }
    }
    
    public boolean execute(String date){

        //date="20060317";
        boolean returnValue = false;
        boolean isAllItemsDownSuccess = false;
        boolean isItemsDoingDownSuccess = false;
        boolean isItemsRefusedDownSuccess = false;
        boolean isItemsFinishedDownSuccess = false;
        boolean isItemsCanceledDownSuccess = false;
        
        boolean isAllItemsExecSuccess = false;
        boolean isItemsDoingExecSuccess = false;
        boolean isItemsRefusedExecSuccess = false;
        boolean isItemsFinishedExecSuccess = false;
        boolean isItemsCanceledExecSuccess = false;
        
        //2: 初始化对帐结果表
        try{
            dao.initAuditResult(date);
        }catch(GSException e){
            GSLogWriter.error(e.getCode(),e.getMessage());
        }
        

        //3.1: 根据当前对帐日期下载ALLITEMS文件
        try{
            download.transactionAllItmesFile(date);
            dao.recordDownloadSucceed(date,1);
            isAllItemsDownSuccess =true;
        }catch(GSException e){
            try{
                dao.recordDownloadFailed(date,GSAuditConstants.ALLITEMS);
            }catch(GSException ee){
                GSLogWriter.error(ee.getCode(),ee.getMessage());    
            }
        }
        //3.2: 根据当前对帐日期下载ITEMSDOING文件
        try{
            download.transactionItemsDoingFile(date);
            dao.recordDownloadSucceed(date,2);
            isItemsDoingDownSuccess = true;
        }catch(GSException e){
            try{
                dao.recordDownloadFailed(date,GSAuditConstants.ITEMSDOING);
            }catch(GSException ee){
                GSLogWriter.error(ee.getCode(),ee.getMessage());    
            }
        }
        //3.3: 根据当前对帐日期下载ITEMSREFUSED文件
        try{
            download.transactionItemsRefusedFile(date);
            dao.recordDownloadSucceed(date,3);
            isItemsRefusedDownSuccess = true;
        }catch(GSException e){
            try{
                dao.recordDownloadFailed(date,GSAuditConstants.ITEMSREFUSED);
            }catch(GSException ee){
                GSLogWriter.error(ee.getCode(),ee.getMessage());    
            }
        }
        //3.4: 根据当前对帐日期下载ITEMSFINISHED文件
        try{
            download.transactionItemsFinishedFile(date);
            dao.recordDownloadSucceed(date,4);
            isItemsFinishedDownSuccess = true;
        }catch(GSException e){
            try{
                dao.recordDownloadFailed(date,GSAuditConstants.ITEMSFINISHED);
            }catch(GSException ee){
                GSLogWriter.error(ee.getCode(),ee.getMessage());        
            }
        }
        //3.5: 根据当前对帐日期下载ITEMSCANCELED文件
        try{
            download.transactionItemsCancelFile(date);
            dao.recordDownloadSucceed(date,4);
            isItemsCanceledDownSuccess = true;
        }catch(GSException e){
            try{
                dao.recordDownloadFailed(date,GSAuditConstants.ITEMSCANCEL);
            }catch(GSException ee){
                GSLogWriter.error(ee.getCode(),ee.getMessage());        
            }
        }
        
        //4.1: 对帐ALLITEMS
        if(isAllItemsDownSuccess){
            try{
                manager.auditAllItems(GSAuditConstants.getFile(date,GSAuditConstants.ALLITEMS),date);
                isAllItemsExecSuccess = true;
            }catch(GSException e){
                GSLogWriter.error(e.getCode(),e.getMessage());    
            }            
        }

        //4.2: 对帐ITEMSDOING	
        if(isItemsDoingDownSuccess){
            try{
                manager.auditItemsDoing(GSAuditConstants.getFile(date,GSAuditConstants.ITEMSDOING),date);
                isItemsDoingExecSuccess = true;                
            }catch(GSException e){
                GSLogWriter.error(e.getCode(),e.getMessage());    
            }            
        }
    
        //4.3: 对帐ITEMSREFUSED
        if(isItemsRefusedDownSuccess){
            try{
                manager.auditItemsRefused(GSAuditConstants.getFile(date,GSAuditConstants.ITEMSREFUSED),date);
                isItemsRefusedExecSuccess = true;                
            }catch(GSException e){
                GSLogWriter.error(e.getCode(),e.getMessage());
            }
        }

        // 4.4: 对帐ITEMSFINISHED
        if(isItemsFinishedDownSuccess){
            try{
                manager.auditItemsFinished(GSAuditConstants.getFile(date,GSAuditConstants.ITEMSFINISHED),date);
                isItemsFinishedExecSuccess = true;                
            }catch(GSException e){
                GSLogWriter.error(e.getCode(),e.getMessage());
            }
        }
        
        // 4.5: 对帐ITEMSCANCELED
        if(isItemsCanceledDownSuccess){
            try{
                manager.auditItemsCanceled(GSAuditConstants.getFile(date,GSAuditConstants.ITEMSCANCEL),date);
                isItemsCanceledExecSuccess = true;
            }catch(GSException e){
                GSLogWriter.error(e.getCode(),e.getMessage());
            }
        }

        returnValue = isItemsFinishedDownSuccess && isItemsRefusedDownSuccess && isItemsDoingDownSuccess && isAllItemsDownSuccess && isItemsCanceledDownSuccess; 
        return returnValue;
        
    }
    
    public static void main(String[] args){
        GSAuditFlowManager m = GSAuditFlowManager.getInstance();
        m.execute("20060317");
    }
}
